(function() {"use strict";var __module = CC_EDITOR ? module : {exports:{}};var __filename = 'preview-scripts/assets/launch.js';var __require = CC_EDITOR ? function (request) {return cc.require(request, require);} : function (request) {return cc.require(request, __filename);};function __define (exports, require, module) {"use strict";
cc._RF.push(module, 'e53e6YiUd9FJLf8yIQu1n47', 'launch', __filename);
// launch.js

'use strict';

//
// api: https://developers.weixin.qq.com/minigame/dev/document/open-api/data/wx.getUserInfo.html
//
var self = void 0;
cc.Class({
    extends: cc.Component,

    properties: {
        tips: cc.Label,
        content: cc.Node,
        prefab: cc.Prefab

    },

    /*onLoad () {
    	self = this;
    },*/

    onLoad: function onLoad() {
        self = this;

        if (typeof wx === 'undefined') {
            return;
        }

        wx.onMessage(function (data) {
            console.log("onMessage command:" + data.command);
            if (data.command == 'getRank') {
                //获取排行榜
                self.initFriendInfo();
            } else if (data.command == 'setScore') {
                //上传分数
                self.uploadWXData(data.maxScore);
            }
        });

        //this.initTips();
        //this.initUserInfo();
        //this.initFriendInfo();
    },
    initTips: function initTips() {
        var renderTypeStr = 'Canvas';
        if (cc.game.renderType === cc.game.RENDER_TYPE_WEBGL) {
            renderTypeStr = 'WEBGL';
        }
        self.tips.string = '\u5F00\u653E\u6570\u636E\u57DF\u5F53\u524D\u652F\u6301 ' + renderTypeStr + ' \u6E32\u67D3\u6A21\u5F0F';
    },
    initUserInfo: function initUserInfo() {
        wx.getUserInfo({
            openIdList: ['selfOpenId'],
            lang: 'zh_CN',
            success: function success(res) {
                console.log("进来initUserInfo");
                self.createUserBlock(res.data[0]);
            },
            fail: function fail(res) {
                console.error(res);
            }
        });
    },
    initFriendInfo: function initFriendInfo() {
        wx.getFriendCloudStorage({
            keyList: ["maxScore"],
            success: function success(res) {
                console.log("进来了initFriendInfo");
                console.log(res);
                //按maxScore从大到小排列
                var data = self.sortList(res.data, false);
                self.content.removeAllChildren();
                for (var i = 0; i < data.length; ++i) {
                    console.log(data[i]);
                    self.createUserBlock(data[i]);
                }
            },
            fail: function fail(res) {
                console.error(res);
            }
        });
    },


    /*onLookGroupBtnClicked(){ //查看群排行按钮(实际是分享)
        cc.log("点击查看群排行按钮");
        cc.loader.loadRes("texture/share",function(err,data){
            wx.shareAppMessage({
                title: "不怕，就来PK！",
                imageUrl: data.url,
                success(res){
                    console.log("转发成功!!!")
                    if(res.shareTickets == null || res.shareTickets == undefined || res.shareTickets == ""){ //没有群信息，说明分享的是个人
                        console.log("排行榜res.shareTickets is null");
                        self.showTipsUI("查看群排行请分享到群"); //自动消失提示框
                    }else{ //有群信息
                        console.log("排行榜res.shareTickets is not null");
                        console.log(res);
                        self.subTitle.string = "群排行榜";
                        if(res.shareTickets.length > 0){
                            console.log("res.shareTickets:" + res.shareTickets);
                            self.lookGroupBtnNode.active = false;
                            wx.postMessage({ message: "groupRank" + res.shareTickets[0] }); //通知子域运行场景groupRank
                        }
                    }
                },
                fail(res){
                    console.log("转发失败!!!")
                } 
            })
        }); 
    },*/

    createUserBlock: function createUserBlock(user) {
        var node = cc.instantiate(self.prefab);
        node.parent = self.content;
        node.x = 0;

        // set nickName
        var userName = node.getChildByName('userName').getComponent(cc.Label);
        userName.string = user.nickName || user.nickname;

        // set maxScore
        var maxScore = node.getChildByName('maxScore').getComponent(cc.Label);
        maxScore.string = 123;
        var dList = user.KVDataList;
        for (var j = 0; j < dList.length; j++) {
            if (dList[j].key == "maxScore") {
                maxScore.string = dList[j].value;
                break;
            }
        }

        // set avatar
        cc.loader.load({ url: user.avatarUrl, type: 'png' }, function (err, texture) {
            if (err) console.error(err);
            var userIcon = node.getChildByName('mask').children[0].getComponent(cc.Sprite);
            userIcon.spriteFrame = new cc.SpriteFrame(texture);
        });
    },


    uploadWXData: function uploadWXData(maxScore) {
        //上报到微信服务器：历史最高分
        console.log("maxScore:" + maxScore);
        var kvDataList = new Array();
        var a = {};
        a["key"] = "maxScore";
        a["value"] = "" + maxScore;
        kvDataList.push(a);
        wx.setUserCloudStorage({
            KVDataList: kvDataList,
            success: function success(res) {
                console.log("存储数据成功");
            },
            fail: function fail(res) {
                console.log("存储数据失败");
            },
            complete: function complete(res) {
                console.log("存储数据完成");
            }
        });
    },

    sortList: function sortList(ListData, order) {
        //排序(ListData：res.data;order:false降序，true升序)
        ListData.sort(function (a, b) {
            var AMaxScore = 0;
            var KVDataList = a.KVDataList;
            for (var i = 0; i < KVDataList.length; i++) {
                if (KVDataList[i].key == "maxScore") {
                    AMaxScore = KVDataList[i].value;
                }
            }

            var BMaxScore = 0;
            KVDataList = b.KVDataList;
            for (var i = 0; i < KVDataList.length; i++) {
                if (KVDataList[i].key == "maxScore") {
                    BMaxScore = KVDataList[i].value;
                }
            }

            if (order) {
                return parseInt(AMaxScore) - parseInt(BMaxScore);
            } else {
                return parseInt(BMaxScore) - parseInt(AMaxScore);
            }
        });
        return ListData;
    }

});

cc._RF.pop();
        }
        if (CC_EDITOR) {
            __define(__module.exports, __require, __module);
        }
        else {
            cc.registerModuleFunc(__filename, function () {
                __define(__module.exports, __require, __module);
            });
        }
        })();
        //# sourceMappingURL=launch.js.map
        